package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 用户审核
     * @param dto
     * @param status
     */
    @Override
    public String auth(AuthDto dto, Short status) {
        if(dto == null || dto.getId() == null){
           throw new RuntimeException("用户审核参数有误");
        }
        //设置参数
        ApUserRealname aurl = new ApUserRealname();
        aurl.setId(dto.getId());
        //失败则设置原因
        if(status == ApUserRealname.Status.FAIL_AUTH.getCode()){
            aurl.setReason(dto.getMsg());
        }
        aurl.setStatus(status);
        updateById(aurl);
        //查询ApUserRealname
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(aurl.getId());
        //成功则返回apuser
        ApUser apUser = null;
        if(status == ApUserRealname.Status.SUCCESS_AUTH.getCode()){
            apUser = apUserMapper.selectById(apUserRealname.getUserId());
        }
        return JSON.toJSONString(apUser);
    }


    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findList(AuthDto dto) {
        //校验
        if(dto == null){
            throw new RuntimeException("aaa");
        }
        dto.checkParam();

        //分页查询条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();



        //状态查询条件
        if(dto.getStatus() != null){
            lqw.eq(ApUserRealname::getStatus,dto.getStatus());
        }

        //查询
        IPage<ApUserRealname> iPage = page(page, lqw);

        //结果返回
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }
}
